package com.yuncheng.oaApi.controller;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaBusinessConstant;
import com.yuncheng.spcyApi.utils.MyExcelUtils;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.OaBgypsxmxxx;
import com.yuncheng.oaApi.service.IOaBgypsxmxxxService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import com.yuncheng.system.api.SystemAPI;
import org.springframework.context.annotation.Lazy;

/**
 * @Description: 办公用品申请明细信息
 * @Author: yuncheng
 * @Date: 2023-08-07
 * @Version: V1.0
 */
@Api(tags = "办公用品申请明细信息接口")
@RestController
@RequestMapping("/api/spzx-oa/oaBgypsxmxxx")
public class OaBgypsxmxxxController extends BaseController<OaBgypsxmxxx, IOaBgypsxmxxxService> {

    private static final Logger log = LoggerFactory.getLogger(OaBgypsxmxxxController.class);

    @Resource
    private IOaBgypsxmxxxService oaBgypsxmxxxService;

    @Resource
    @Lazy
    private SystemAPI systemAPI;

    @Resource
    private MyExcelUtils oaMyExcelUtils;

    /**
     * 分页列表查询
     *
     * @param oaBgypsxmxxx
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "办公用品申请明细信息-分页列表查询")
    @ApiOperation(value = "办公用品申请明细信息-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(OaBgypsxmxxx oaBgypsxmxxx, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<OaBgypsxmxxx> queryWrapper = QueryBuilder.initQueryWrapper(oaBgypsxmxxx, req.getParameterMap());
        Page<OaBgypsxmxxx> page = new Page<>(pageNo, pageSize);
        IPage<OaBgypsxmxxx> pageList = oaBgypsxmxxxService.page(page, queryWrapper);
        for(OaBgypsxmxxx vo:pageList.getRecords()){
            formatValue(vo);
        }
        return HttpResult.ok(pageList);
    }



    /**
     * 添加
     *
     * @param oaBgypsxmxxx
     * @return
     */
    @AutoLog(value = "办公用品申请明细信息-添加")
    @ApiOperation(value = "办公用品申请明细信息-添加")
    //@RequiresPermissions("bgypsqmxxx:OaBgypsxmxxx:add")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody OaBgypsxmxxx oaBgypsxmxxx) {
        HttpResult<String> result = new HttpResult(true);
        oaBgypsxmxxxService.save(oaBgypsxmxxx);
        result.setResult(oaBgypsxmxxx.getId());
        result.setMessage(I18nUtil.message("save.success"));
        return result;
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "办公用品申请明细信息-通过id查询")
    @ApiOperation(value = "办公用品申请明细信息-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        OaBgypsxmxxx oaBgypsxmxxx = oaBgypsxmxxxService.getById(id);
        return HttpResult.ok(oaBgypsxmxxx);
    }



    /**
     * 数据转换
     * @param oaBgypsxmxxx
     */
    private void formatValue(OaBgypsxmxxx oaBgypsxmxxx){
    }

    //导出申请明细
    @AutoLog(value = "导出申请明细记录数据-表格excel")
    @ApiOperation(value = "导出申请明细记录数据-表格excel")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids",value = "主键id字符逗号分割",required = true)
    })
    @GetMapping("/exportSqmxData")
    public void exportSqmxData(@RequestParam(value = "ids",defaultValue = "")String ids,
                                OaBgypsxmxxx ndkpBasic,
                                HttpServletResponse response){
        try {
            String name = "exportSqmxData";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name+".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

            List<String> IdsList = Arrays.asList(ids.split(","));
            List<Object> list = new ArrayList<>(); // 导出的数据
            // 多选导出
            if (StringUtils.isNotEmpty(ids)) {
                List<OaBgypsxmxxx> ndkpBasicList = oaBgypsxmxxxService.listByIds(IdsList);
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaBgypsxmxxx e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }else {
                LambdaQueryWrapper<OaBgypsxmxxx> queryWrapper = new LambdaQueryWrapper<>();
                //申请人
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFsqr()),OaBgypsxmxxx::getFsqr,ndkpBasic.getFsqr());
                //申请名称
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFsqmc()),OaBgypsxmxxx::getFsqmc,ndkpBasic.getFsqmc());

                if (ndkpBasic.getCreateTime() != null){
                    queryWrapper.like(OaBgypsxmxxx::getCreateTime,ndkpBasic.getCreateTime());
                }

                List<OaBgypsxmxxx> ndkpBasicList = oaBgypsxmxxxService.list(queryWrapper);
                // 条件导出
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaBgypsxmxxx e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }
            oaMyExcelUtils.outputListToExclFile(OaBusinessConstant.IMPORT_OA_BGYPSQMX_XML,list,out); // 导出模板数据成excel
        } catch (Exception e) {
            log.error("导出申请明细数据出错-{}",e.getMessage());
        }
    }

}
